home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0049_HACKING in Pascal.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  1KB  |  57 lines

  1. {
  2. KAI ROHRBACHER
  3.  
  4. I'm looking For a way to tell BorlandPascal that an allocated _data_
  5. block should now be treated as an executable routine (in Protected Mode).
  6. Here is a little example to show the problem; it runs w/o problems in
  7. Real Mode, but results in a GP-fault (despite the use of the alias-selector!):
  8. }
  9.  
  10. Program SelfModify;
  11.  
  12. Const
  13.   AnzNOPs = 10;
  14.  
  15. Type
  16.   TTestProc = Procedure;
  17.  
  18. Var
  19.   code : Pointer;
  20.   Run  : TTestProc;
  21.   pb   : ^Byte;
  22.   pw   : ^Word Absolute pb;
  23.   i    : LongInt;
  24.  
  25. begin
  26.   GetMem(code, AnzNOPs + 7); {7 Bytes For proc header & end}
  27.   pb := code; {pb = ^start of routine to build}
  28.  
  29.   pb^ := $55;
  30.   INC(pb);   {push bp}
  31.   pw^ := $E589;
  32.   INC(pw); {mov bp,sp}
  33.   For i := 1 to AnzNOPs DO
  34.   begin
  35.     pb^ := $90;
  36.     INC(pb); {nop's}
  37.   end;
  38.   pb^ := $5D;
  39.   INC(pb);   {pop bp}
  40.   pb^ := $CA;
  41.   INC(pb);
  42.   pw^ := $0000;          {retf 0}
  43.  
  44.   {$IFDEF DPMI}
  45.   WriteLN('Protected Mode');
  46.   code:= Ptr(Seg(code) + SelectorInc, Ofs(code)); {alias-selector}
  47.   {$else}
  48.   WriteLN('Real Mode');
  49.   {$endIF}
  50.  
  51.   Run := TTestProc(code); {that's a Type-cast!}
  52.   Run; {call routine}
  53.  
  54.   FreeMem(code, AnzNOPs + 7);
  55.   WriteLN('Alive and kicking!');
  56. end.
  57.